package com.bjpowernode.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.shop.base.BaseStore;
import com.bjpowernode.shop.constants.CommonConstants;
import com.bjpowernode.shop.domain.IndexImg;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/indexImg")
public class IndexImgController extends BaseStore {

    @GetMapping("/page")
    public ResponseEntity<Page<IndexImg>> page(Page<IndexImg> page, Integer status) {
        return ResponseEntity.ok(
                indexImgService.page(
                        page,
                        new LambdaQueryWrapper<IndexImg>().eq(
                                ObjectUtils.isNotEmpty(status), IndexImg::getStatus, status
                        )
                ));
    }

    @PostMapping
    @Transactional
    public ResponseEntity<Boolean> save(@RequestBody IndexImg indexImg) {
        return ResponseEntity.ok(indexImgService.save(
                indexImg.setShopId(CommonConstants.DEFAULT_SHOP_ID)
                        .setUploadTime(new Date())
        ));
    }
}
